package com.example.tys.myplayer;

import android.util.Log;
import android.view.Surface;
import android.view.SurfaceHolder;
import android.view.SurfaceView;

/**
 * Created by david on 2017/9/20.
 */

public class DavidPlayer implements SurfaceHolder.Callback {
    static {
        System.loadLibrary("avcodec");
        System.loadLibrary("avdevice");
        System.loadLibrary("avfilter");
        System.loadLibrary("avformat");
        System.loadLibrary("avutil");
        System.loadLibrary("postproc");
        System.loadLibrary("swresample");
        System.loadLibrary("swscale");
        System.loadLibrary("native-lib");
    }

    private SurfaceView surfaceView;

    public void playJava(String path) {
        if (surfaceView == null) {
            return;
        }
        play(path);
    }

    public void setSurfaceView(SurfaceView surfaceView) {
        this.surfaceView = surfaceView;
//        display(surfaceView.getHolder().getSurface(), width, height);
        surfaceView.getHolder().addCallback(this);

    }

    public native void play(String path);

    public native void display(Surface surface, int width, int height);


    public native void release();


    @Override
    public void surfaceCreated(SurfaceHolder surfaceHolder) {

    }

    @Override
    public void surfaceChanged(SurfaceHolder holder, int format, int width, int height) {
        display(holder.getSurface(), width, height);
        Log.e("---", width + "x" + height);
    }

    @Override
    public void surfaceDestroyed(SurfaceHolder surfaceHolder) {

    }

    public void pause() {
        play_pause();
    }

    private native void play_pause();
}
